package com.holystone.orca.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateFomatUtil {
	/**日期默认格式*/
	public static final String defaultFormat = "yyyy-MM-dd HH:mm:ss";

	public static final String TIME_YEAR = "yyyy";

	public static final String TIME_MONEN = "MM";

	public static final String TIME_DAY = "dd";
	/**
	 * 格式化日期
	 * @author: 李海宁
	 * @date: 2015年12月3日 下午2:50:26
	 * @param format 日期格式
	 * @param date 要格式化的日期
	 * @return String 格式化后的日期字符串
	 */
	public static String formatDate(String format,Date date){
		if(format==null||format.equals("")){
			format = defaultFormat;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * @author:fent
	 * 将字符串类型转换为时间类型
	 * @param str
	 * @return
	 */
	public static Date str2Date(String str) {
		Date d = null;
		SimpleDateFormat sdf = new SimpleDateFormat(defaultFormat);
		try {
			d = sdf.parse(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * @author:fent
	 * 将字符串类型转换为时间类型
	 * @param str
	 * @param pattern
	 * @return
	 */
	public static Date str2Date(String str, String pattern) {
		Date d = null;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			d = sdf.parse(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * @author:fent
	 * 日期类型转换为字符串类型
	 * @param date
	 * @return
	 */
	public static String date2Str(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(defaultFormat);
		return sdf.format(date);
	}

	/**
	 * @author:fent
	 * 时间戳转换为字符串
	 * @param timestamp
	 * @return
	 */
	public static String timestampToStr(long timestamp){
		SimpleDateFormat sdf=new SimpleDateFormat(defaultFormat);
		return sdf.format(new Date(timestamp));
	}

	/**
	 * @author:fent
	 * 获取昨天
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static Date getLastDate(Date date) {
		Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
		calendar.setTime(date);
		calendar.add(calendar.DATE, -1);
		return str2Date(date2Str(calendar.getTime()));
	}

	/**
	 * @author:fent
	 * 获取前几天
	 * @param date
	 * @return
	 */
	public static Date getBeforeDate(Date date,int dates) {
		Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
		calendar.setTime(date);

		calendar.add(calendar.DATE, -dates);

		return str2Date(date2Str(calendar.getTime()));
	}
	
	
	public static String formatTimestampToStr(String format,long timestamp){
		return formatDate(format,new Date(timestamp));
	}

	/**
	 * 将字符串转换为时间戳
	 * 字符串格式为 yyyy-MM-dd hh:mm:ss
	 * @param time
	 * @return
	 */
	public static long formatStrToLong(String time){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date;
		long timestamp = 0l;
		try {
			date = df.parse(time);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			timestamp = cal.getTimeInMillis();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return timestamp;
		
	}

}
